package behavioralPattern.iterator.example01;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * 具体迭代器角色
 * @param <E>
 */
public class ConcreteIterator<E> implements Iterator{

    private int cursor;

    private List<E> list;

    public ConcreteIterator(List<E> list) {
        this.cursor = 0;
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return cursor != list.size();
    }

    @Override
    public void next() {
        cursor ++;
    }

    @Override
    public Object currentItem() {
        if (cursor >= list.size()){
            throw new NoSuchElementException();
        }
        return list.get(cursor);
    }
}
